package com.mpp.hosp.controller.api;

import com.mpp.common.exception.YyghException;
import com.mpp.common.helper.HttpRequestHelper;
import com.mpp.common.result.ResultCodeEnum;
import com.mpp.common.util.Md5Utils;
import com.mpp.hosp.service.DepartmentService;
import com.mpp.hosp.service.HospitalService;
import com.mpp.hosp.service.HospitalSetService;
import com.mpp.hosp.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author MMDZ
 * @date 2022/8/19 23:12
 * @description TODO
 */
public class BaseController {
    @Autowired
    protected HospitalService hospitalService;
    @Autowired
    protected HospitalSetService hospitalSetService;
    @Autowired
    protected DepartmentService departmentService;
    @Autowired
    protected ScheduleService scheduleService;

    // 校验签名步骤
    protected Map<String, Object> CheckSignStep(HttpServletRequest request) {
        // 获取传递过来医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        // 获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        // 获取签名
        String hospSign = (String) paramMap.get("sign");
        // 根据传递过来医院编码，查询数据库签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        // 把数据库查询签名进行MD5加密
        String signKeyMd5 = Md5Utils.encrypt(signKey);
        // 判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        return paramMap;
    }
}
